﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Collections;
using System.Data;
using Pixysoft.Framework.Configurations;
using System.Xml.Serialization;

namespace Pixysoft.Framework.Noebe.MemoryCaches.Core
{
    class CacheDataTable : ICacheDataTable
    {
        //实际存放缓存的地点 日后使用基础框架替代

        MostRecentlyUsedList<ICacheDataRow> mrulist = null;

        //缓存名 针对不同的sql和查询参数设定

        string cacheTableName;

        //广义表名 针对sql

        string tableName;

        List<CacheDataRowCell> schemas = new List<CacheDataRowCell>();


        public CacheDataTable()
        {
            this.mrulist = new MostRecentlyUsedList<ICacheDataRow>();
        }

        public CacheDataTable(int capacity, string cacheTableName, DataTable table)
        {
            this.cacheTableName = cacheTableName;

            this.tableName = table.TableName;

            foreach (DataColumn column in table.Columns)
            {
                schemas.Add(new CacheDataRowCell(
                    column.ColumnName, column.DataType.FullName));
            }

            this.mrulist = new MostRecentlyUsedList<ICacheDataRow>(capacity);
        }


        public bool Contains(string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            foreach (ICacheDataRow row in mrulist)
            {
                if (row.CacheKey.Equals(key))
                    return true;
            }

            return false;
        }


        public System.Data.DataTable Get(string key)
        {
            DataTable table = new DataTable(TableName);

            if (string.IsNullOrEmpty(key))
                return table;

            foreach (CacheDataRowCell cell in schemas)
            {
                table.Columns.Add(cell.ColumnName, Type.GetType(cell.DataType));
            }

            foreach (ICacheDataRow cacherow in mrulist[key])
            {
                cacherow.Hit();

                cacherow.GetDataRow(table);
            }

            return table;
        }

        public System.Data.DataTable GetAll()
        {
            DataTable table = new DataTable(TableName);

            foreach (CacheDataRowCell cell in schemas)
            {
                table.Columns.Add(cell.ColumnName, Type.GetType(cell.DataType));
            }

            foreach (ICacheDataRow cacherow in mrulist)
            {
                cacherow.Hit();

                cacherow.GetDataRow(table);
            }

            return table;
        }


        public bool IsOverdued(string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            DateTime now = Pixysoft.Tools.GlobalTimer.Instance.GetGlobalTime();

            foreach (ICacheDataRow cacherow in mrulist[key])
            {
                if (cacherow.EndDate < now)
                    return true;
            }

            return false;
        }


        public void AddShallowCache(string key, System.Data.DataTable table)
        {
            AddShallowCache(key, StringHelper.default_cache_timespan, table);
        }

        public void AddShallowCache(string key, int timeSpan, System.Data.DataTable table)
        {
            if (string.IsNullOrEmpty(key))
                return;

            Clear(key);

            foreach (DataRow row in table.Rows)
            {
                ShallowCacheDataRow datarow = new ShallowCacheDataRow(
                    key, timeSpan, row);

                mrulist.Add(key, datarow);
            }
        }


        public void AddShallowCache(string key, params System.Data.DataRow[] rows)
        {
            AddShallowCache(key, StringHelper.default_cache_timespan, rows);
        }

        public void AddShallowCache(string key, int timeSpan, params System.Data.DataRow[] rows)
        {
            if (string.IsNullOrEmpty(key))
                return;

            Clear(key);

            foreach (DataRow row in rows)
            {
                ShallowCacheDataRow datarow = new ShallowCacheDataRow(
                    key, timeSpan, row);

                mrulist.Add(key, datarow);
            }
        }


        public void AddNullCache(string key)
        {
            AddNullCache(key, StringHelper.default_cache_timespan);
        }

        public void AddNullCache(string key, int timeSpan)
        {
            if (string.IsNullOrEmpty(key))
                return;

            Clear(key);

            mrulist.Add(key, new NullCacheDataRow(key, timeSpan));
        }


        public void AddDeepCache(string key, DataTable table)
        {
            AddDeepCache(key, StringHelper.default_cache_timespan, table);
        }

        public void AddDeepCache(string key, int timeSpan, DataTable table)
        {
            if (string.IsNullOrEmpty(key))
                return;

            Clear(key);

            foreach (DataRow row in table.Rows)
            {
                DeepCacheDataRow datarow = new DeepCacheDataRow(
                    key, timeSpan, row);

                mrulist.Add(key, datarow);
            }
        }


        public void AddDeepCache(string key, params DataRow[] rows)
        {
            AddDeepCache(key, StringHelper.default_cache_timespan, rows);
        }

        public void AddDeepCache(string key, int timeSpan, params DataRow[] rows)
        {
            if (string.IsNullOrEmpty(key))
                return;

            Clear(key);

            foreach (DataRow row in rows)
            {
                DeepCacheDataRow datarow = new DeepCacheDataRow(
                    key, timeSpan, row);

                mrulist.Add(key, datarow);
            }
        }


        public bool SchemaIsConfirmed(DataTable schema)
        {
            //首先寻找缓存有的 表结构没有的

            foreach (CacheDataRowCell cell in schemas)
            {
                if (!schema.Columns.Contains(cell.ColumnName))
                    return false;
            }

            //如果第一条符合，则最起码schema的列数据等于当前，否则一定是有多余的column

            if (schemas.Count != schema.Columns.Count)
                return false;

            return true;
        }


        public void Clear(string key)
        {
            if (string.IsNullOrEmpty(key))
                return;

            mrulist.Remove(key);
        }

        public void ClearAll()
        {
            mrulist.Clear();
        }

        public bool Verify()
        {
            if (string.IsNullOrEmpty(TableName))
                return false;

            if (string.IsNullOrEmpty(CacheTableName))
                return false;

            return true;
        }



        [XmlAttribute("cachename")]
        public string CacheTableName
        {
            get { return cacheTableName; }
            set { cacheTableName = value; }
        }

        [XmlAttribute("tablename")]
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        [XmlAttribute("capacity")]
        public int Capacity
        {
            get
            {
                return mrulist.Capacity;
            }
            set
            {
                mrulist.Capacity = value;
            }
        }

        public List<DeepCacheDataRow> DataRows
        {
            get
            {
                List<DeepCacheDataRow> rlist = new List<DeepCacheDataRow>();
                foreach (ICacheDataRow datarow in mrulist)
                {
                    if (datarow is DeepCacheDataRow)
                        rlist.Add(datarow as DeepCacheDataRow);
                }
                return rlist;
            }
            set
            {
                ClearAll();

                foreach (DeepCacheDataRow datarow in value)
                {
                    mrulist.Add(datarow.CacheKey, datarow);
                }
            }
        }

        public List<CacheDataRowCell> Schemas
        {
            get
            {
                return schemas;
            }
            set
            {
                schemas = value;
            }
        }

        [XmlIgnore]
        public int Count
        {
            get { return mrulist.Count; }
        }
    }
}
